<template>
  <div class="login-container">
    <el-form
      :rules="rules"
      :model="ruleForm"
      ref="ruleForm"
      label-width="100px"
      class="demo-dynamic"
    >
      <el-form-item
        prop="email"
        label="邮箱"
        :rules="[
      { required: true, message: '请输入邮箱地址', trigger: 'blur' },
      { type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change'] }
    ]"
      >
        <el-input v-model="ruleForm.email"></el-input>
      </el-form-item>
      <el-form-item label="验证码" prop="valicode">
        <el-input type="text" v-model="ruleForm.valicode" autocomplete="off"></el-input>
        <button @click="sendValidateCode()">发送验证码</button>
      </el-form-item>
      <el-form-item label="昵称" prop="nickname">
        <el-input type="text" v-model="ruleForm.nickname" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="pass">
        <el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="checkPass">
        <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">注册</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
export default {
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.ruleForm.checkPass !== "") {
          this.$refs.ruleForm.validateField("checkPass");
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.ruleForm.pass) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };

    return {
      ruleForm: {
        email: "",
        valicode: "",
        pass: "",
        checkPass: ""
      },
      rules: {
        pass: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { validator: validatePass, trigger: "blur" }
        ],
        checkPass: [
          { required: true, message: "请再次确认密码", trigger: "blur" },
          { validator: validatePass2, trigger: "blur" }
        ],
        valicode: [
          {
            required: true,
            message: "请输入验证码",
            trigger: "blur"
          }
        ]
      },
      validateCode: ""
    };
  },
  methods: {
    submitForm(formName) {
      let _this = this;
      this.$refs[formName].validate(valid => {
        console.log(123, _this.validateCode, _this.ruleForm.valicode);
        if (valid && _this.validateCode == _this.ruleForm.valicode) {
          _this.regist();
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    regist() {
      console.log("注册", this.ruleForm);
      let _this = this;
      this.$axios
        .post("user/regist", {
          nickname: this.ruleForm.nickname,
          password: this.ruleForm.pass,
          email: this.ruleForm.email
        })
        .then(function(res) {
          console.log("注册结果", res);
          if (res.status == 200) {
            alert("注册成功！");
            _this.$router.push("/Login");
          } else {
            alert("发布失败");
          }
        })
        .catch(function(error) {
          console.log(error);
        });
    },
    sendValidateCode() {
      let regEmail = /^[A-Za-z1-9]+([-_.][A-Za-z1-9]+)*@([A-Za-z1-9]+[-.])+[A-Za-z]{2,5}$/;
      if (!regEmail.test(this.ruleForm.email)) {
        console.log("邮箱格式不正确");
        return;
      } else {
        this.getValidateCode(this.ruleForm.email);
      }
    },
    getValidateCode(email) {
      let _this = this;
      this.$axios
        .get("/getValicode", {
          params: { email: email }
        })
        .then(function(res) {
          if (res.data != "") {
            _this.validateCode = res.data;
            alert("验证码已发送到邮箱！");
          }
        })
        .catch(function(error) {
          console.log(error);
        });
    }
  }
};
</script>

<style scoped>
.login-container {
  width: 40%;
  min-width: 300px;
  height: 300px;
  margin: 35px auto;
}
</style>
